package com.tiger.anim 
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	/**
	 * 位图元素列表
	 * @author wmTiger
	 */
	public class BmdArray 
	{
		private static const STR_KF:String = "kf";
		
		private var mArray:Vector.<BmdElement>;
		private var mActions:Dictionary = new Dictionary();
		private var mLength:int = 0
		
		public function BmdArray(mc:MovieClip) 
		{
			mArray = new Vector.<BmdElement>();
			createArray(mc);
		}
		
		public function get length():int
		{
			return mLength;
		}
		
		public function getElementByIdx(idx:int):BmdElement
		{
			return mArray[idx];
		}
		
		public function getFrameByLabel(label:String):int
		{
			return (mActions[label] == null ? 1 : mActions[label].frame);
		}
		
		private function createArray(mc:MovieClip):void 
		{
			var frames:int = mc.totalFrames;
			mLength = frames;
			//trace(mLength);
			var rect:Rectangle;
			var bmd:BitmapData;
			var obj:Object;
			var mx:Matrix = new Matrix();
			var i:int = 0;
			var labels:Array = mc.currentLabels;
			var dic_kf:Dictionary = new Dictionary();
			for (i = 0; i < labels.length; i++) 
			{
				if (labels[i].name.substr(0, 2) == STR_KF) 
				{
					dic_kf[labels[i].frame] = labels[i];
				}
				else
				{
					mActions[labels[i].name] = labels[i];
				}
			}
			for (i = 1; i <= frames; i++) 
			{
				mc.gotoAndStop(i);
				
				if (dic_kf[i] == null) 
				{
					mArray.push(null);
					continue;
				}
				
				rect = mc.getBounds(mc);
				bmd = new BitmapData(rect.width, rect.height, true, 0);
				mx.tx = -rect.x;
				mx.ty = -rect.y;
				bmd.draw(mc, mx);
				obj = { x:rect.x, y:rect.y, bmd:bmd };
				mArray.push(new BmdElement(obj));
			}
		}
		
	}

}